// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {colorList} from "./bgColorControl";
import AudioManage, {soundList} from "../framework/AudioManage";
import settingMediator from "../framework/settingMediator";

const {ccclass, property} = cc._decorator;

export enum boxColor {
    red =  1 ,
    blue =  2,
    green = 3,
    orange = 4,
    purple = 5,
}

let boxClosePicUrl = ["dj-x03a","dj-x04a","dj-x01a","dj-x02a","dj-x05a"];
let boxOpenPicUrl = ["dj-x03b","dj-x04b","dj-x01b","dj-x02b","dj-x05b"];

@ccclass
export default class boxMediator extends cc.Component {

    @property({
        type : cc.Enum(boxColor),
        displayName : "宝箱的颜色"
    })
    curBoxColor : boxColor = boxColor.red;

    @property({type : cc.Node,displayName : "钥匙节点"})
    keyNode : cc.Node = null;

    @property()
    dieBox : boolean = false;

    @property()
    showGuide : boolean = false;

    @property({type : cc.Component.EventHandler,displayName : "触发事件"})
    toolEvent : cc.Component.EventHandler[] = [];

    boxOpened : boolean = false;

    onLoad () {
        this.setBoxSkin(boxClosePicUrl[this.curBoxColor -1]);
    }

    setBoxSkin(url){
        cc.loader.loadRes("img/level/" + url,cc.SpriteFrame,(error, resource) => {
            if(error){
                console.log(error);
                return;
            }
            this.node.getComponent(cc.Sprite).spriteFrame = resource;
        })
    }

    onBeginContact(contact, self, other) {
        if(this.boxOpened) return;
        if(other.node.group == "role"){
            let roleKey = other.node.getComponent("roleMediator").curProcessKey;
            if(this.curBoxColor == roleKey){
                this.boxOpened = true;
                this.setBoxSkin(boxOpenPicUrl[this.curBoxColor - 1]);
                other.node.getComponent("roleMediator").curProcessKey = null;
                other.node.getComponent("roleMediator").setRoleProcessKey(null);
                if(this.dieBox){
                    AudioManage.playSFX(soundList.openlock,false);
                    cc.game.emit("setColor",colorList.red);
                    cc.game.emit("setTitle");
                    cc.game.emit("GameOver");
                    settingMediator.doVibration(2);
                    cc.game.emit("shieldPressKey",true);
                    let spk = cc.find("Canvas/role").getChildByName("spine").getComponent(sp.Skeleton);
                    spk.setAnimation(0,"die",false);
                    this.scheduleOnce(()=>{
                        cc.game.emit("gameLose");
                    },2);
                    console.log("游戏结束");
                }else{
                    AudioManage.playSFX(soundList.openlock,false);
                    if(other.node.x - this.node.x <= 0){
                        this.keyNode.x = 80;
                    }else{
                        this.keyNode.x = -80;
                    }
                    this.keyNode.active = true;

                    if(this.showGuide){
                        cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
                    }
                }
            }else{
                console.log("钥匙不对");
            }
        }
    }
}
